<?php

namespace app\service;

use app\model\Category;
use app\model\UserVip;
use app\model\VipConfig;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

class HomeService
{
    public static ?HomeService $_instance = null;

    /**
     * @return HomeService|null
     */
    public static function instance(): ?HomeService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /**
     * @return string[][][]
     */
    public function converterArr(): array
    {
        return [];
        $where[]        = ['status', '=', 1];
        $field          = 'base_type,name,title,desc,img,multiple,extensions,accept,alt_title,detail_desc';
        $list           = Category::getList($where, $field, 'sort desc,id asc', 1, 50, 3600);
        $_category_list = [];
        foreach ($list as $value) {
            $base_type = $value['base_type'];
            $name      = $value['name'];
            unset($value['name']);
            unset($value['base_type']);
            $value['multiple']                 = $value['multiple'] == 1;
            $_category_list[$base_type][$name] = $value;
        }
        return $_category_list;
    }

    public function vipConfig(int $osType = 0): array
    {
        $where[] = ['id', '>', 0];
        if ($osType) $where[] = ['os_type', '=', $osType];
        $field = 'id,day,sort,type,desc,old_money,money,status,times,save_day,content';
        $list  = VipConfig::getList($where, $field, 'sort asc', 1, 10, 3600);
        if ($list) {
            $type_array = UserVip::$vip_type_array;
            foreach ($list as &$value) {
                $value['title']   = is_string($value['type']) ? $value['type'] : $type_array[$value['type']];
                $value['content'] = urlencode($value['content'] ?: '');
            }
            return $list;
        }
        return [];
    }

    public function vip_show(int $osType = 0): array
    {
        $vip_config = self::instance()->vipConfig($osType);
        $result     = [];
        foreach ($vip_config as $value) {
            if ($value['status'] != 1) continue;
            $result[] = $value;
        }
        return $result;
    }

    public function vip_detail(string $vip_type = ''): array
    {
        $detail     = [];
        $vip_config = self::instance()->vipConfig();
        if (empty($vip_config)) return $detail;
        foreach ($vip_config as $value) {
            if ($value['type'] == $vip_type) $detail = $value;
        }
        return $detail;
    }

    /**
     * @param int $max_length
     * @return bool|string
     */
    public function setCaptcha(int $max_length = 4): bool|string
    {
        $phrase = new PhraseBuilder($max_length, '0123456789');
        // 初始化验证码类
        $builder = new CaptchaBuilder($phrase->build($max_length), $phrase);
        // 生成验证码
        $builder->build();
        // 将验证码的值存储到session中
        request()->session()->set('captcha', strtolower($builder->getPhrase()));
        // 获得验证码图片二进制数据
        return $builder->get();
    }

    /**
     * 获取二级域名前缀
     *
     * @return string
     */
    public function get_second_host(): string
    {
        $host       = request()->host();
        $host_array = explode('.', $host);
        return count($host_array) >= 3 ? $host_array[0] : WEB_WWW;
    }
}